package yx.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;

import yx.model.BsConfig;
import yx.service.ConfigServiceInt;
import yx.service.CourseServiceInt;
import yx.vo.CourseVO;
import yx.vo.DatagridVO;
import yx.vo.JsonVO;

import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("basePackage")
@Namespace("/")
@Action(value = "courseAction")
public class CourseAction extends BaseAction implements ModelDriven<CourseVO> {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(CourseAction.class);

	private CourseVO course = new CourseVO();

	@Override
	public CourseVO getModel() {
		return course;
	}

	private CourseServiceInt courseService;
	private ConfigServiceInt configService;

	public CourseServiceInt getCourseService() {
		return courseService;
	}

	@Autowired
	public void setCourseService(CourseServiceInt courseService) {
		this.courseService = courseService;
	}

	public ConfigServiceInt getConfigService() {
		return configService;
	}

	@Autowired
	public void setConfigService(ConfigServiceInt configService) {
		this.configService = configService;
	}
	
	public void queryCourseList() {
		DatagridVO datagridVO = courseService.datagrid(course);
		if(datagridVO.getTotal().intValue() != 0){
			List list = datagridVO.getRows();
			super.writeJson(list);
		} else {
			super.writeJson(null);
		}
	}

	public void queryCourse() {
		super.writeJson(courseService.datagrid(course));
	}

	public void addCourse() {
		CourseVO tempCourse = new CourseVO();
		tempCourse.setCourseName(course.getCourseName());
		DatagridVO vo = courseService.datagrid(tempCourse);
		// 判断课程名称是否存在，存在提示错误，不存在，保存到数据库
		if (vo.getTotal() != null && vo.getTotal().intValue() != 0) {
			JsonVO j = new JsonVO();
			j.setSuccess(false);
			j.setMsg("新增的课程已经存在！");
			super.writeJson(j);
		} else {
			JsonVO j = new JsonVO();
			try {
				CourseVO u = courseService.save(course);
				j.setSuccess(true);
				j.setMsg("增加成功！");
				j.setObj(u);
			} catch (Exception e) {
				j.setMsg(e.getMessage());
			}
			super.writeJson(j);
		}
	}

	public void removeCourse() {
		courseService.remove(course.getIds());
		JsonVO j = new JsonVO();
		j.setSuccess(true);
		j.setMsg("删除成功！");
		super.writeJson(j);
		
		// TODO : 引用的课程不可删除
		/*
		List<BsConfig> refList = configService.getRefTable();
		if (refList != null) {
			String tableName = "";
			String fieldName = "";
			for (BsConfig t : refList) {
				if (t.getType().equals(tree.getType())) {
					tableName = t.getTableName();
					fieldName = t.getFieldName();
					break;
				}
			}
			if ("".equals(tableName) && "".equals(fieldName)) {
				// 配置表中对应的类型为空，可以删除
				treeService.removeById(tree.getId().toString());
				JsonVO j = new JsonVO();
				j.setSuccess(true);
				j.setMsg("删除成功！");
				super.writeJson(j);
			} else if (!"".equals(tableName) && "".equals(fieldName)) {
				// 配置表配置有问题
				JsonVO j = new JsonVO();
				j.setSuccess(false);
				j.setMsg("配置表的配置存在问题，请联系管理员！");
				super.writeJson(j);
			} else if ("".equals(tableName) && !"".equals(fieldName)) {
				// 配置表配置有问题
				JsonVO j = new JsonVO();
				j.setSuccess(false);
				j.setMsg("配置表的配置存在问题，请联系管理员！");
				super.writeJson(j);
			} else {
				// 配置表配置正确
				Boolean flag = configService.isRefTable(tree.getId(), tableName, fieldName);
				if (flag) {
					JsonVO j = new JsonVO();
					j.setSuccess(false);
					j.setMsg("数据被引用，不允许删除！");
					super.writeJson(j);
				} else {
					treeService.removeById(tree.getId().toString());
					JsonVO j = new JsonVO();
					j.setSuccess(true);
					j.setMsg("删除成功！");
					super.writeJson(j);
				}
			}
		} else {
			// 配置表为空，可以删除
			treeService.removeById(tree.getId().toString());
			JsonVO j = new JsonVO();
			j.setSuccess(true);
			j.setMsg("删除成功！");
			super.writeJson(j);
		}
		*/
	}

	public void editCourse() {
		CourseVO u = courseService.edit(course);
		JsonVO j = new JsonVO();
		j.setSuccess(true);
		j.setMsg("编辑成功！");
		j.setObj(u);
		super.writeJson(j);
	}

}
